<template>
  <div class="shopinfo-container">
    <!-- 小球 -->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <!-- istrue用来判断类名是否为true -->
          <swiper :lunbolist="lunbolist" :istrue="false"></swiper>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">{{shopinfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>
            市场价：
            <span class="sp1">￥{{shopinfo.market_price}}</span>&nbsp;&nbsp;
            销售价：
            <span class="sp2">￥{{shopinfo.sell_price}}</span>
          </p>
          <p>
            购买数量：
            <numbox @getNum="getNum" :max="shopinfo.stock_quantity"></numbox>
          </p>
          <mt-button type="primary" size="small" @click="shopping">立即购买</mt-button>
          <mt-button type="danger" size="small" @click="ballAddShop">加入购物车</mt-button>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>
            商品货号：
            <span>{{shopinfo.goods_no}}</span>
          </p>
          <p>
            库存情况：
            <span>{{shopinfo.stock_quantity}}件</span>
          </p>
          <p>
            上架时间：
            <span>{{shopinfo.add_time | dateformat | updata}}</span>
          </p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="imgtext(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="shopcom(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
// 导入swiper公用的子组件
import swiper from "../public/swiper";
import numbox from "../public/numbox";
export default {
  name: "ShopInfo",
  data() {
    return {
      id: this.$route.params.id,
      lunbolist: [], //轮播图数据
      shopinfo: {},//当前页面数据
      ballFlag: false,//小球初始化不显示
      bollNum:1
    };
  },
  mounted() {
    this.getlunbo();
    this.getshopinfo();
  },
  methods: {
    // 获取轮播图
    getlunbo() {
      this.$http.get("api/getthumimages/" + this.id).then(result => {
        let data = result.body;
        if (data.status === 0) {
          data.message.forEach(item => {
            item.img = item.src;
          });
          this.lunbolist = data.message;
        }
      });
    },
    // 获取商品详情
    getshopinfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(result => {
        let data = result.body;
        if (data.status === 0) {
          // console.log(data.message);
          this.shopinfo = data.message[0];
        }
      });
    },
    // 跳转图文介绍
    imgtext(id) {
      this.$router.push({ name: "shoprecom", params: { id } });
    },
    // 跳转评论
    shopcom(id) {
      this.$router.push({ name: "shopcomment", params: { id } });
    },
    // 加入购物车
    ballAddShop() {
      this.ballFlag = !this.ballFlag;
      //  把这个传到仓储{ id:商品的id, count: 要购买的数量, price: 商品的单价，selected: false  }
      var goodinfo={id:this.id,count:this.bollNum,price:this.shopinfo.sell_price,selected:true};
      this.$store.commit('addShop', goodinfo);
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;//强制动画刷新
        // 获取小球的横纵坐标
      const rec=this.$refs.ball.getBoundingClientRect();//返回一个对象
        //   console.log(rec) 
        // etBoundingClientRect方法用于获得页面中某个元素的左，上，右和下分别相对浏览器视窗window的位置。
      const rec1=document.getElementById('spanid').getBoundingClientRect();
      const x=rec1.left-rec.left;
      const y=rec1.top-rec.top;
        // 'translate('+x+'px,'+y+'px)'
      el.style.transform = `translate(${x}px,${y}px)`;
      el.style.transition = "all .5s cubic-bezier(0.250, 0.250, 0.750, 0.750)  ";
      done();//调用afterEnter
    },
    afterEnter(el) {
         this.ballFlag = !this.ballFlag;
    },
    // 获取子组件的购物数值
    getNum(num){
      this.bollNum=num;
      console.log(this.bollNum);
    },
    // 立即购买
    shopping(){
      var goodinfo={id:this.id,count:this.bollNum,price:this.shopinfo.sell_price,selected:true};
      this.$store.commit('addShop', goodinfo);
      this.$router.push('/shop');
      
    }
  },
  components: {
    swiper,
    numbox
  }
};
</script>

<style lang="scss" scoped>
.shopinfo-container {
  margin-bottom: 1rem;
  overflow: hidden;
  background-color: #eee;
  // 小球样式
  .ball {
    width: 0.3rem;
    height: 0.3rem;
    background-color: #f00;
    border-radius: 50%;
    position: absolute;
    z-index: 99;
    top: 7.31rem;
    left: 3rem;
  }
  .mui-card {
    .sp1 {
      text-decoration: line-through;
    }
    .sp2 {
      color: #f00;
      font-weight: 700;
      font-size: 0.32rem;
    }
  }
  .mui-card-header {
    font-weight: 700;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 0.3rem 0;
    }
  }
}
</style>